﻿#include "StdAfx.h"
#include "CodeUtils.h"


CCodeUtils::CCodeUtils(void)
{
}


CCodeUtils::~CCodeUtils(void)
{
}

// 注释：多字节包括GBK和UTF-8  
int CCodeUtils::GBK2UTF8(char *szGbk,char *szUtf8,int Len)  
{  
    // 先将多字节GBK（CP_ACP或ANSI）转换成宽字符UTF-16  
    // 得到转换后，所需要的内存字符数  
    int n = MultiByteToWideChar(CP_ACP,0,szGbk,-1,NULL,0);  
    // 字符数乘以 sizeof(WCHAR) 得到字节数  
    WCHAR *str1 = new WCHAR[sizeof(WCHAR) * n];  
    // 转换  
    MultiByteToWideChar(CP_ACP,  // MultiByte的代码页Code Page  
        0,            //附加标志，与音标有关  
        szGbk,        // 输入的GBK字符串  
        -1,           // 输入字符串长度，-1表示由函数内部计算  
        str1,         // 输出  
        n             // 输出所需分配的内存  
        );  
  
    // 再将宽字符（UTF-16）转换多字节（UTF-8）  
    n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL);  
    if (n > Len)  
    {  
        delete[]str1;  
        return -1;  
    }  
    WideCharToMultiByte(CP_UTF8, 0, str1, -1, szUtf8, n, NULL, NULL);  
    delete[]str1;  
    str1 = NULL;  
  
    return 0;  
}

//UTF-8 GBK  
int CCodeUtils::UTF82GBK(char *szUtf8,char *szGbk,int Len)  
{  
    int n = MultiByteToWideChar(CP_UTF8, 0, szUtf8, -1, NULL, 0);  
    WCHAR * wszGBK = new WCHAR[sizeof(WCHAR) * n];  
    memset(wszGBK, 0, sizeof(WCHAR) * n);  
    MultiByteToWideChar(CP_UTF8, 0,szUtf8,-1, wszGBK, n);  
  
    n = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);  
    if (n > Len)  
    {  
        delete[]wszGBK;  
        return -1;  
    }  
  
    WideCharToMultiByte(CP_ACP,0, wszGBK, -1, szGbk, n, NULL, NULL);  
  
    delete[]wszGBK;  
    wszGBK = NULL;  
  
    return 0;  
}

//Unicode到UTF-8的转换
int CCodeUtils::UnicodeToUtf8(const char* unicode, int size, char* utf8)
{
wchar_t* wstr = new wchar_t[size/2+1];
memcpy(wstr, unicode, size);
int len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, utf8, len, NULL, NULL);
if(wstr) delete[] wstr;
return len;
}

wchar_t* CCodeUtils::Utf8ToUnicode(const char* utf8)
{
	int len = ::MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
    wchar_t *tmp = new wchar_t[len*2+1];
    MultiByteToWideChar(CP_UTF8, 0, utf8, -1, tmp, len);
    return tmp;
}


char* CCodeUtils::WCharToChar(WCHAR *s)
{
	int w_nlen=WideCharToMultiByte(CP_ACP,0,s,-1,NULL,0,NULL,false);
	char *ret=new char[w_nlen];
	memset(ret,0,w_nlen);
	WideCharToMultiByte(CP_ACP,0,s,-1,ret,w_nlen,NULL,false);
	return ret;
}

WCHAR * CCodeUtils::charToWchar(char *s){
	int w_nlen=MultiByteToWideChar(CP_ACP,0,s,-1,NULL,0);
	WCHAR *ret;
	ret=(WCHAR*) malloc(sizeof(WCHAR)*w_nlen);
	memset(ret,0,sizeof(ret));
	MultiByteToWideChar(CP_ACP,0,s,-1,ret,w_nlen);
	return ret;
}

void CCodeUtils::CStringToChar(CString str,char *dst){
	
#ifdef _UNICODE
	wchar_t*wChar=str.GetBuffer(str.GetLength());
	str.ReleaseBuffer();
	// 将得到的wchar* 类型转为 char*类型
	size_t len = wcslen(wChar) + 1;
	size_t converted = 0;
	char *cChar;
	cChar = (char*)malloc(len * sizeof(char));
	wcstombs_s(&converted, cChar, len, wChar, _TRUNCATE);
	strcpy(dst,cChar);
	free(cChar);

#else
    CString cstr(str);
    char *p = (LPSTR)(LPCSTR)str1;
    // 第二种
    使用 GetBuffer方法返回 char * 类型

        CString str1 = "123";

    char *t1 = str1.GetBuffer(str1.GetLength());

    str1.ReleaseBuffer();

#endif
}

char* CCodeUtils::UnicodeToGB2312(const wchar_t* szUnicodeString)
{
    UINT nCodePage = 936;//GB2312
    int nLength=WideCharToMultiByte(nCodePage,0,szUnicodeString,-1,NULL,0,NULL,NULL);
    char* pBuffer=new char[nLength+1];
    WideCharToMultiByte(nCodePage,0,szUnicodeString,-1,pBuffer,nLength,NULL,NULL);
    pBuffer[nLength]=0;
    return pBuffer;
}


wchar_t* CCodeUtils::GB2312ToUnicode(const char* szGBString){
	 UINT nCodePage = 936;//GB2312
    int nLength=MultiByteToWideChar(nCodePage,0,szGBString,-1,NULL,0);
    wchar_t* pBuffer =new wchar_t[nLength+1];
    MultiByteToWideChar(nCodePage,0,szGBString,-1,pBuffer,nLength);
    pBuffer[nLength]=0;
    return pBuffer;
}

char* CCodeUtils::UnicodeToMBytes(const wchar_t *lpwcszWString){
	char *pElementText;
	int iTextLen;
	// wide char to multi char
	iTextLen = ::WideCharToMultiByte(CP_ACP, 0, lpwcszWString,  - 1, NULL, 0, NULL, NULL);
	pElementText = new char[iTextLen + 1];
	memset((void *)pElementText, 0, (iTextLen + 1) *sizeof(char));
	::WideCharToMultiByte(CP_ACP, 0, lpwcszWString, 0, pElementText, iTextLen, NULL, NULL);
	return pElementText;
}

wchar_t* CCodeUtils::StringToChar(CString& str)
{
	wchar_t *tr = str.GetBuffer(str.GetLength());
	str.ReleaseBuffer();
	return tr; 

}